package com.xx.ems.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.xx.ems.model.Accessory;
import com.xx.ems.model.Equipments;
import com.xx.ems.service.EquipmentCRUDService;
import com.xx.ems.system.BaseService;

@Service(value="equipmentCRUDService")
public class EquipmentCRUDServiceImpl extends BaseService implements EquipmentCRUDService {
	@Override
	public List<Integer> addEquipment(int enternum, Equipments equipments, List<Accessory> accessoryList) {
		try {
			getTransaction().begin();
			List<Integer> eidarray = getEquipmentCRUDDao().addEquipment(enternum, equipments, accessoryList);
			getTransaction().commit();
			return eidarray;
		}catch(Exception e){
			e.printStackTrace();
			getTransaction().rollback();
			return null;
		}
	}

	@Override
	public boolean updateEquipment(Equipments equipments, List<Accessory> accessoryList) {
		try {
			getTransaction().begin();
			getEquipmentCRUDDao().updateEquipment(equipments, accessoryList);
			getTransaction().commit();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			getTransaction().rollback();
			return false;
		}
	}

	@Override
	public boolean deleteEquipment(int[] eids) {
		// TODO Auto-generated method stub
		return true;
		
	}

	@Override
	public boolean deleteAccessory(String aids) {
		try {
			getTransaction().begin();
			getEquipmentCRUDDao().deleteAccessory(aids.split(","));
			getTransaction().commit();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			getTransaction().rollback();
			return false;
		}
	}

	@Override
	public void bindEimgWithEquipment(List<Integer> eidArray, String eimgPath) {
		try {
			getTransaction().begin();
			getEquipmentCRUDDao().bindEimgWithEquipment(eidArray, eimgPath);
			getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			getTransaction().rollback();
		}
	}

	@Override
	public Map<String, Object> getEquipmentList(int pageNow, int maxCount) {
		return getEquipmentCRUDDao().getEquipmentList(pageNow, maxCount);
	}

	@Override
	public List<Object> getAccessoryByEquipmentId(int eid) {
		return getEquipmentCRUDDao().getAccessoryByEquipmentId(eid);
	}

	@Override
	public Map<String, Object> getEquipmentListByEnumberOrEname(String searchinfo, int pageNow, int maxCount) {
		return getEquipmentCRUDDao().getEquipmentListByEnumberOrEname(searchinfo, pageNow, maxCount);
	}

	@Override
	public Map<String, Object> getEquipmentListByCriteriaQuery(int pageNow, int maxCount, Equipments equipment, double eprice2, Date in_time2, Date produced_date2, int deadline2, Date buy_time2) {
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			getTransaction().begin();
			map = getEquipmentCRUDDao().getEquipmentListByCriteriaQuery(pageNow, maxCount, equipment, eprice2, in_time2, produced_date2, deadline2, buy_time2);
			getTransaction().commit();
			return map;
		}catch (Exception e) {
			e.printStackTrace();
			getTransaction().rollback();
			map = null;
			return map;
		}
	}
}